package com.ws.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.bean.User;
import com.ws.bean.vo.UserVo;
import com.ws.service.FavoriteService;
import com.ws.service.UserService;
import com.ws.utils.Res;
import com.ws.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/homeUsers")
public class UserController {
    @Autowired
    private UserService userService;
    //收藏
    @Autowired
    private FavoriteService favoriteService;
    //查询所有
    @RequestMapping("list")
    public String list(Model model, UserVo userVo) {

        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(userVo.getCurrentPage(),userVo.getPageSize());
        List<User> list =userService.list(userVo);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("userVo",userVo);
        return "home/user/list";
    }



    //跳转到添加页面
    @RequestMapping("toadd")
    public String toAdd() {
        return "home/user/add";
    }
    //添加
    @RequestMapping("add")
    public String add(User user, MultipartFile image) throws IOException {
        if (image.getSize()>0){
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            String houZhui = image.getOriginalFilename().split("\\.")[1];
            user.setUser_tx(uuid+"."+houZhui);
            image.transferTo(new File(Res.fileDir+uuid+"."+houZhui));
        }
        user.setUser_pass(SecurityUtils.encrypt("12345"));
        userService.add(user);
        return "redirect:/homeUsers/list";
    }
    //删除
    @RequestMapping("delete")
    public String delete(Integer user_id,String tx){
        File file = new File(Res.fileDir+tx);
        if (file.exists()){
            file.delete();
        }
        int delete = favoriteService.deleteUaserFavorite(user_id);
        userService.delete(user_id);
        return "redirect:/homeUsers/list";
    }
    //修改
    @RequestMapping("toUpdate")
    public String toUpdate(Integer user_id,Model model ) {
        User user = userService.selUserOne(user_id);
        model.addAttribute("user",user);
        return "home/user/update";
    }
    @RequestMapping("update")
    public String update(User user, MultipartFile image,String tx) throws IOException {
        if (image.getSize()>0){
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            String houZhui = image.getOriginalFilename().split("\\.")[1];
            user.setUser_tx(uuid+"."+houZhui);
            image.transferTo(new File(Res.fileDir+uuid+"."+houZhui));

            User olduser = userService.selUserOne(user.getUser_id());
            String oldTx = olduser.getUser_tx();

            if (oldTx!=null&&!oldTx.equals(user.getUser_tx())){
                File oldFile = new File(Res.fileDir+oldTx);
                if (oldFile.exists()){
                    oldFile.delete();
                }
            }
        }else{
            user.setUser_tx(tx);
        }
        user.setUser_pass(SecurityUtils.encrypt(user.getUser_pass()));
        int update = userService.update(user);
        return "redirect:/homeUsers/list";
    }
}
